package com.example.camerademo;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import java.io.File;

import ai.hou.camera.util.BitmapUtils;
import ai.hou.camera.util.CameraConfig;

/**
 * @author zhaoxiaolei
 */
public class DemoActivity extends AppCompatActivity {

    public static final int REQUEST_CODE = 1;
    private ImageView ivPicture;
    private CheckBox cbCataract;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo);

        ivPicture = findViewById(R.id.iv_picture);
        cbCataract = findViewById(R.id.cb_cataract);
        findViewById(R.id.btn_cataract).setOnClickListener(v -> {
            //其他颜色白内障
            Intent intent = CameraConfig.newIntent();
            intent.putExtra(CameraConfig.EXTRA_TORCH, true);
            intent.putExtra(CameraConfig.EXTRA_TURNOVER, true);
            intent.putExtra(CameraConfig.EXTRA_SUPPORT_ZOOM, true);
            intent.putExtra(CameraConfig.EXTRA_ASPECT_RATIO, true);
            intent.putExtra(CameraConfig.EXTRA_HAS_MASK_VIEW, true);
            intent.putExtra(CameraConfig.EXTRA_MASK_TYPE, CameraConfig.MASK_ID_CARD_FRONT);
            intent.putExtra(CameraConfig.EXTRA_HAS_CROP_VIEW, true);
            intent.putExtra(CameraConfig.EXTRA_HAS_CATARACT, true);
            intent.putExtra(CameraConfig.EXTRA_CATARACT_COLOR, Color.parseColor("#66FF0000"));
            startActivityForResult(intent,REQUEST_CODE);
        });
        findViewById(R.id.btn_default).setOnClickListener(v -> {
            //默认，无遮罩，无裁剪
            Intent intent = CameraConfig.newIntent();
            startActivityForResult(intent,REQUEST_CODE);
        });
        findViewById(R.id.btn_only_picture).setOnClickListener(v -> {
            //拍照直接保存到相册
            Intent intent = CameraConfig.newIntent();
            intent.putExtra(CameraConfig.EXTRA_SAVE_DIRECTLY, true);
            startActivityForResult(intent,REQUEST_CODE);
        });
        findViewById(R.id.btn_path).setOnClickListener(v -> {
            //存储到指定目录及名字
            File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "指定路径名.jpg");
            Intent intent = CameraConfig.newIntent();
            intent.putExtra(CameraConfig.EXTRA_PICTURE_PATH, file.getAbsolutePath());
            startActivityForResult(intent,REQUEST_CODE);
        });
        findViewById(R.id.btn_qrcode).setOnClickListener(v -> {
            //支持扫描二维码
            Intent intent = CameraConfig.newIntent();
            intent.putExtra(CameraConfig.EXTRA_SUPPORT_SCAN_QR_CODE, true);
            startActivityForResult(intent,REQUEST_CODE);
        });
        findViewById(R.id.btn_qrcode_back).setOnClickListener(v -> {
            //扫描二维码返回
            Intent intent = CameraConfig.newIntent();
            intent.putExtra(CameraConfig.EXTRA_SUPPORT_SCAN_QR_CODE, true);
            intent.putExtra(CameraConfig.EXTRA_QR_CODE_BACK, true);
            startActivityForResult(intent,REQUEST_CODE);
        });
        findViewById(R.id.btn_mask_picture).setOnClickListener(v -> {
            //遮罩直接保存到相册
            Intent intent = CameraConfig.newIntent();
            intent.putExtra(CameraConfig.EXTRA_HAS_MASK_VIEW, true);
            intent.putExtra(CameraConfig.EXTRA_MASK_TYPE, CameraConfig.MASK_STANDARD_CARD);
            intent.putExtra(CameraConfig.EXTRA_SAVE_DIRECTLY, true);
            intent.putExtra(CameraConfig.EXTRA_HAS_CATARACT, cbCataract.isChecked());
            startActivityForResult(intent,REQUEST_CODE);
        });
        findViewById(R.id.btn_mask_crop_idcard_front).setOnClickListener(v -> {
            //身份证国徽遮罩+裁剪
            Intent intent = CameraConfig.newIntent();
            intent.putExtra(CameraConfig.EXTRA_TORCH, true);
            intent.putExtra(CameraConfig.EXTRA_TURNOVER, true);
            intent.putExtra(CameraConfig.EXTRA_SUPPORT_ZOOM, true);
            intent.putExtra(CameraConfig.EXTRA_ASPECT_RATIO, true);
            intent.putExtra(CameraConfig.EXTRA_HAS_MASK_VIEW, true);
            intent.putExtra(CameraConfig.EXTRA_MASK_TYPE, CameraConfig.MASK_ID_CARD_FRONT);
            intent.putExtra(CameraConfig.EXTRA_HAS_CROP_VIEW, true);
            intent.putExtra(CameraConfig.EXTRA_HAS_CATARACT, cbCataract.isChecked());
            startActivityForResult(intent,REQUEST_CODE);
        });
        findViewById(R.id.btn_mask_crop_idcard_back).setOnClickListener(v -> {
            //身份证人像遮罩+裁剪
            Intent intent = CameraConfig.newIntent();
            intent.putExtra(CameraConfig.EXTRA_TORCH, true);
            intent.putExtra(CameraConfig.EXTRA_TURNOVER, true);
            intent.putExtra(CameraConfig.EXTRA_SUPPORT_ZOOM, true);
            intent.putExtra(CameraConfig.EXTRA_ASPECT_RATIO, true);
            intent.putExtra(CameraConfig.EXTRA_HAS_MASK_VIEW, true);
            intent.putExtra(CameraConfig.EXTRA_MASK_TYPE, CameraConfig.MASK_ID_CARD_BACK);
            intent.putExtra(CameraConfig.EXTRA_HAS_CROP_VIEW, true);
            intent.putExtra(CameraConfig.EXTRA_HAS_CATARACT, cbCataract.isChecked());
            startActivityForResult(intent,REQUEST_CODE);
        });
        findViewById(R.id.btn_mask_crop_card).setOnClickListener(v -> {
            //银行卡等遮罩+裁剪
            Intent intent = CameraConfig.newIntent();
            intent.putExtra(CameraConfig.EXTRA_TORCH, true);
            intent.putExtra(CameraConfig.EXTRA_TURNOVER, true);
            intent.putExtra(CameraConfig.EXTRA_SUPPORT_ZOOM, true);
            intent.putExtra(CameraConfig.EXTRA_ASPECT_RATIO, true);
            intent.putExtra(CameraConfig.EXTRA_HAS_MASK_VIEW, true);
            intent.putExtra(CameraConfig.EXTRA_MASK_TYPE, CameraConfig.MASK_STANDARD_CARD);
            intent.putExtra(CameraConfig.EXTRA_HAS_CROP_VIEW, true);
            intent.putExtra(CameraConfig.EXTRA_HAS_CATARACT, cbCataract.isChecked());
            startActivityForResult(intent,REQUEST_CODE);
        });
        findViewById(R.id.btn_drag_crop_enable).setOnClickListener(v -> {
            //禁止拖动裁剪框
            Intent intent = CameraConfig.newIntent();
            intent.putExtra(CameraConfig.EXTRA_TORCH, true);
            intent.putExtra(CameraConfig.EXTRA_TURNOVER, true);
            intent.putExtra(CameraConfig.EXTRA_SUPPORT_ZOOM, true);
            intent.putExtra(CameraConfig.EXTRA_ASPECT_RATIO, true);
            intent.putExtra(CameraConfig.EXTRA_HAS_MASK_VIEW, true);
            intent.putExtra(CameraConfig.EXTRA_MASK_TYPE, CameraConfig.MASK_STANDARD_CARD);
            intent.putExtra(CameraConfig.EXTRA_HAS_CROP_VIEW, true);
            intent.putExtra(CameraConfig.EXTRA_HAS_CATARACT, cbCataract.isChecked());
            intent.putExtra(CameraConfig.EXTRA_DRAG_CROP_ENABLE, false);
            startActivityForResult(intent,REQUEST_CODE);
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK && data!=null) {
            String dataTakeType = CameraConfig.getDataTakeType(data);
            String text = CameraConfig.getDataTakeString(data, dataTakeType);
            if (CameraConfig.DATA_TAKE_CAMERA.equals(dataTakeType) || CameraConfig.DATA_TAKE_ALBUM.equals(dataTakeType)) {
                DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
                Bitmap bitmap = BitmapUtils.adjustSize(text, displayMetrics.widthPixels, displayMetrics.heightPixels);
                ivPicture.setImageBitmap(bitmap);
                Toast.makeText(this, "已保存到：" + text, Toast.LENGTH_SHORT).show();
            }else if (CameraConfig.DATA_TAKE_QR_CODE.equals(dataTakeType)) {
                showDecodeQrCodeResult(text);
            }
        }
    }

    private void showDecodeQrCodeResult(String msg) {
        new AlertDialog.Builder(this)
                .setTitle("温馨提示")
                .setMessage(msg)
                .setPositiveButton("我知道了", null)
                .show();
    }
}